//
//  LabelTFCell.m
//  PlayCatchGame
//
//  Created by 流言 on 2018/2/27.
//  Copyright © 2018年 清正. All rights reserved.
//

#import "LabelTFCell.h"
#import "AppHeader.h"

@interface LabelTFCell() <UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *textField;
@property (weak, nonatomic) IBOutlet UIButton *button;
@property (weak, nonatomic) IBOutlet UILabel *label;

@property (copy, nonatomic) ButtonBlock buttonBlock;
@property (copy, nonatomic) TFChangeBlock TFBlock;

@end

@implementation LabelTFCell

- (void)awakeFromNib {
    [super awakeFromNib];
}

- (void)setViewType:(RightViewType)viewType {
    if (_viewType != viewType) {
    _viewType =viewType;
    
        if (_viewType ==RightViewType_TextField) {
            self.textField.hidden =NO;
            self.button.hidden =YES;
            self.label.hidden =YES;
        } else if (_viewType ==RightViewType_Button) {
            self.textField.hidden =YES;
            self.button.hidden =NO;
            self.label.hidden =YES;
        } else if (_viewType ==RightViewType_Label) {
            self.textField.hidden =YES;
            self.button.hidden =YES;
            self.label.hidden =NO;
        }
    }
}



- (void)setDataDict:(NSMutableDictionary *)dataDict {
    _dataDict =dataDict;
    
    if (_viewType ==RightViewType_TextField) {
        if ([[dataDict allKeys] containsObject:@"title"]) {
            self.titleLabel.text =dataDict[@"title"];
        }
        if ([[dataDict allKeys] containsObject:@"placeholder"]) {
            self.textField.placeholder =dataDict[@"placeholder"];
            if ([self.textField.placeholder containsString:@"电话"]||
                [self.textField.placeholder containsString:@"手机号"]) {
                self.textField.keyboardType =UIKeyboardTypeNumberPad;
            } else {
                self.textField.keyboardType =UIKeyboardTypeDefault;
            }
        }
        if ([[dataDict allKeys] containsObject:@"content"]) {
            self.textField.text =dataDict[@"content"];
        }
    } else if (_viewType ==RightViewType_Button) {
        if ([[dataDict allKeys] containsObject:@"title"]) {
            self.titleLabel.text =dataDict[@"title"];
        }
        NSString *placeholder =@"";
        if ([[dataDict allKeys] containsObject:@"placeholder"]) {
            placeholder =stringEmpty(dataDict[@"placeholder"]);
        }
        NSString *content =@"";
//        if ([[dataDict allKeys] containsObject:@"content"]) {
            content =stringEmpty(dataDict[@"content"]);
//        }
        if (isStringEmpty(content)) {
            [self.button setTitleColor:[UIColor colorWithRed:0.86 green:0.85 blue:0.86 alpha:1.00] forState:UIControlStateNormal];
            [self.button setTitle:placeholder forState:UIControlStateNormal];
        } else {
            [self.button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [self.button setTitle:content forState:UIControlStateNormal];
        }
    } else if (_viewType ==RightViewType_Label) {
        NSString *placeholder =@"";
        if ([[dataDict allKeys] containsObject:@"placeholder"]) {
            placeholder =stringEmpty(dataDict[@"placeholder"]);
        }
        NSString *content =@"";
        if ([[dataDict allKeys] containsObject:@"content"]) {
            content =stringEmpty(dataDict[@"content"]);
        }
        if (isStringEmpty(content)) {
            self.label.textColor =[UIColor colorWithRed:0.86 green:0.85 blue:0.86 alpha:1.00];
            self.label.text =placeholder;
        } else {
            self.label.textColor =[UIColor blackColor];
            self.label.text =content;
        }
    }
}


/// 创建cell
+ (instancetype)findCellWithIdentifier:(NSString*)identifier {
    NSArray *cellArray =[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil];
    for (LabelTFCell *cell in cellArray) {
        if ([cell.reuseIdentifier isEqualToString:identifier]) {
            return cell;
        }
    }
    return nil;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    if (self.TFBlock) {
        self.TFBlock(textField);
    }
}

- (IBAction)buttonClick:(UIButton *)sender {
    if (self.buttonBlock) {
        self.buttonBlock(sender);
    }
}

- (void)didButtonBlock:(ButtonBlock)newBlock {
    self.buttonBlock = newBlock;
}

- (void)TFEndChangeValueBlock:(TFChangeBlock)newBlock {
    self.TFBlock = newBlock;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
